<?php 
/******* Seteo de Cookies y Paramtros ***************************/
//Seteo de parametros
$avisosXcat = 5;
$diasAntes  = 0;

if(!empty($_POST['avisos_x_cat'])){	
	$avisosXcat = $_POST['avisos_x_cat'];	
	setcookie("avisos_x_cat", $avisosXcat, time()+864000);
}elseif(!empty($_COOKIE["avisos_x_cat"])){	
	$avisosXcat  = $_COOKIE['avisos_x_cat'];
}

if(isset($_POST['dias_antes'])){
	$diasAntes  = $_POST['dias_antes'];
	setcookie("dias_antes", $diasAntes, time()+864000);	
}elseif(!empty($_COOKIE["dias_antes"])){
	$diasAntes  = $_COOKIE['dias_antes'];	
}

$limit = 200;
if(!empty($_REQUEST['current_page']))
	$currentPage  = $_REQUEST['current_page'];
else	
    $currentPage = 0; //pagina inicial
    
$condiciones = array();

if(isset($_REQUEST['less']))
	$currentPage--; 
if(isset($_REQUEST['more']))
	$currentPage++;

if(!empty($_REQUEST['user_email']))
	$email  = $_REQUEST['user_email'];
else	
    $email = ''; //pagina inicial

if(!empty($email))
    $condiciones[] = "email like '%$email%' ";
/**********************END parametros*****************************************/
require("adm_header.php"); 
ini_set("max_execution_time","1000");

if(isset($_POST['setOptions'])){
	writeNewsIntoFile($_POST['noticias']);
}
$contentNews = file_get_contents(NEWSLETTER_NEWS_FILE);

?>
<? writeCommonAdminTable(LA_ADMIN_NEWSL_TITLE,1); ?>
<!-- ********************************************************** -->
<?php
$startRow = $currentPage*$limit;
/***************************************************************************************************/
	//Obtenemos los mercados posibles
	$arrayOfInfoMercados = getArrayOfCatImages();
	$mercadosIdArray = array(
		1  => 'trans',
		13 => 'agro',
		16 => 'vial',
		17 => 'grua',
		1002 => 'serv'
	);
	$mercados = array();
	foreach ($mercadosIdArray as $mercadoId => $inputId){
	
		$mercados[$mercadoId] = array(
			'idInput' 	   => $inputId,
			'cat_image'    => $arrayOfInfoMercados[$mercadoId]['cat_image'],
			'cat_fatherid' => $arrayOfInfoMercados[$mercadoId]['cat_fatherid'],
			'cat_name' 	   => $arrayOfInfoMercados[$mercadoId]['cat_name']
		);	
	}
	$arrayOfUser = getUsersFromNewsletterTableWhere($condiciones, array( 0 => "email ASC"),$startRow,$limit);
	      
?>
<table>
<tbody>  
  <tr>
    <td colspan="2"><b>Opciones:</b></td>
  </tr>   
  <tr>
  	<td colspan="2" rowspan="<?php echo count($mercados); ?>"> 
    	<form action="adm_newsletter2.php" method="post">
    	<table>
    		<tbody>
    			<tr>	
    				<td>Cantidad de Avisos por categoria: </td>
    				<td><input value="<?php echo $avisosXcat?>" type="text" name="avisos_x_cat" /></td>
    				<td> (trae los más nuevos)</td>
    			</tr>
    			<tr>	
    				<td>Traer avisos no más viejos de días: </td>
    				<td><input value="<?php echo $diasAntes?>" type="text" name="dias_antes" /></td>
    				<td> (0 implica todos)</td>
    			</tr>    			
    			<tr>	
    				<td>Noticias: </td>
    				<td colspan="2" >
    					<textarea rows="10" cols="60" name="noticias"><?php if(!empty($contentNews)); echo $contentNews;?></textarea>    				
    				</td>    				
    			</tr>
    			<tr>
    				<td colspan="3">
    					<input value="<?php echo $currentPage?>"  type="hidden" name="current_page" />
    						<input type="hidden" value="<?php echo $email ?>" name="user_email"/>
    					<input type="submit" value="Aplicar" name="setOptions" />
    				</td>
    			</tr>
    		</tbody>
    	</table>
    	</form>    	    
    </td>
  </tr>
</tbody>
</table>
<table>
<tbody>  
  <tr>
    <td colspan="2"><b>Mercados:</b> </td>
  </tr>   
  <tr>
  	<td colspan="2" rowspan="<?php echo count($mercados); ?>"> 
    	<form action="adm_newsletter3.php" method="post">
    	<table>    	
    	<?php foreach($mercados as $idMercado => $value): ?>
    	<tr>
    		<td><input value="1" name="<?php echo $value['idInput']; ?>" type="checkbox" /></td><td><?php echo $value['cat_name']; ?></td>
    	</tr>	
    	<?php endforeach;?>
    	<tr>
    		<td colspan="2">
    			<input type="hidden" value="<?php echo $email ?>" name="user_email"/>
    			<input value="<?php echo $avisosXcat?>" type="hidden" name="avisos_x_cat" />
    			<input value="<?php echo $diasAntes?>"  type="hidden" name="dias_antes" />
    			<input value="<?php echo $currentPage?>"  type="hidden" name="current_page" />
    			<input type="submit" value="Mostrar Newsletter" name="showNewsletter" />
    		</td>
    	</tr>
    	</table>
    	</form>    	    
    </td>
  </tr>
</tbody>
</table>
<table>
<tbody>    
  <tr>
    <td colspan="2"><b>Usuarios:</b></td>    
  </tr>
  <tr>
  		<td colspan="2" rowspan="<?php echo count($mercados)*2; ?>"> 
		  	<form action="adm_newsletter2.php" method="post">
		    <table>
		    	<tbody>		    	
		    	<tr>
		    		<td style="text-align: right;">Buscar usuario por email: </td>
		    		<td style="width: 200px;"><input type="text" value="<?php echo $email ?>" name="user_email"/>
		    		</td>
		    		<td style="left: 1px;">
		    			<input type="submit" name="userSearch" value="Buscar" />
		    		</td>
		    	</tr>
		    	<tr>
		    		<td colspan="3">
		    			<div style="overflow:auto; background-color: #cccccc; border: 1px solid; border-color: #000000;height: 300px;">
		    			<?php foreach($arrayOfUser as $user): ?>
		    				<a href="adm_newsletter3.php?email=<?php echo $user['email']?>" ><?php echo $user['email']?></a>,
		    			<?php endforeach;?>
		    			</div>		    		
		    		</td>
		    	</tr>
		    	<tr>
		    		<td style="text-align: right;">
		    			<?php if($currentPage != 0):?>		    			
		    			<input type="submit" name="less" value="&lt;&lt; Anteriores" />
		    			<?php endif;?>
		    		</td>
		    		<td style="width: 200px;"></td>
		    		<td style="left: 1px;">
		    		<?php if(count($arrayOfUser) == $limit):?>		    			
		    			<input type="submit" name="more" value="Siguientes &gt;&gt;" />		    			
		    		<?php endif;?>	    		
		    	
		    		<input value="<?php echo $avisosXcat?>" type="hidden" name="avisos_x_cat" />
    				<input value="<?php echo $diasAntes?>"  type="hidden" name="dias_antes" />
    				<input value="<?php echo $currentPage?>"  type="hidden" name="current_page" />
		    		</td>
		    	</tr>			    	
		    	</tbody>
		    </table>		    
		  	</form>
  		</td>
  </tr>
</tbody>  
</table>


<!-- ********************************************************** -->
<? writeCommonAdminTable("Email Users",""); ?><!-- END Table menu -->
<? require("adm_footer.php"); ?>